﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq.Expressions;
using System.Text; 
using System.Web.Routing;
using System.Web.Mvc;
using System.Reflection;

namespace SRF.UI.Html
{
    /// <summary>
    /// 
    /// </summary>
    public static class JsonExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="list"></param>
        /// <param name="getId"></param>
        /// <param name="getPId"></param>
        /// <param name="getName"></param>
        /// <param name="rootNode"></param>
        /// <returns></returns>
        public static MvcHtmlString TreeJsonFor<TModel>(this HtmlHelper<TModel> htmlHelper, IList<TModel> list, Func<TModel, string> getId, Func<TModel, string> getPId, Func<TModel, string> getName, string rootNode = "", Action<TModel, IDictionary<string, object>> getAttrs = null)
        {
            string nodeFormat = "{{id:'{0}',pId:{1},name:'{2}'{3}}},";
            string html = "", pid, attrstr = "";

            if (list != null)
            {
                if (getId != null)
                {
                    foreach (TModel m in list)
                    {
                        pid = getPId(m);
                        if (string.IsNullOrEmpty(pid) && !string.IsNullOrEmpty(rootNode))
                            pid = "null";
                        else
                            pid = "'" + pid + "'";
                        if (getAttrs != null)
                        {
                            var attrs = new Dictionary<string, object>();
                            getAttrs(m, attrs);
                            attrstr = "";
                            foreach (KeyValuePair<string, object> kv in attrs)
                            {
                                attrstr += string.Format(",'{0}':'{1}'", kv.Key, kv.Value.ToString());
                            }
                        }
                        html += string.Format(nodeFormat, getId(m), pid, getName(m), attrstr);
                    }
                }
            }

            if (!string.IsNullOrEmpty(rootNode)) //根节点
            {
                html = string.Format("{{id:null,pId:'',name:'{0}'}},", rootNode) + html;
            }
            html = "[" + html.TrimEnd(',') + "]";

            return new MvcHtmlString(html);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="varname"></param>
        /// <param name="list"></param>
        /// <param name="getId"></param>
        /// <param name="getPId"></param>
        /// <param name="getName"></param>
        /// <param name="rootNode"></param>
        /// <param name="getAttrs"></param>
        /// <returns></returns>
        public static string TreeJson<TModel>(this HtmlHelper htmlHelper, string varname, IList<TModel> list, Func<TModel, string> getId, Func<TModel, string> getPId, Func<TModel, string> getName, string rootNode = "", Action<TModel, IDictionary<string, object>> getAttrs = null)
        {
            return TreeJson<TModel>(varname, list, getId, getPId, getName, rootNode, getAttrs);
        }
        public static string TreeJson<TModel>(string varname, IList<TModel> list, Func<TModel, string> getId, Func<TModel, string> getPId, Func<TModel, string> getName, string rootNode = "", Action<TModel, IDictionary<string, object>> getAttrs = null)
        {
            string nodeFormat = "{{id:'{0}',pId:{1},name:'{2}'{3}}},";
            string html = "", pid, attrstr = "";

            if (list != null)
            {
                if (getId != null)
                {
                    foreach (TModel m in list)
                    {
                        if (getPId == null)
                            pid = null;
                        else
                            pid = getPId(m);
                        if (string.IsNullOrEmpty(pid) && !string.IsNullOrEmpty(rootNode))
                            pid = "null";
                        else
                            pid = "'" + pid + "'";
                        if (getAttrs != null)
                        {
                            var attrs = new Dictionary<string, object>();
                            getAttrs(m, attrs);
                            attrstr = "";
                            foreach (KeyValuePair<string, object> kv in attrs)
                            {
                                attrstr += string.Format(",'{0}':'{1}'", kv.Key, kv.Value.ToString());
                            }
                        }
                        html += string.Format(nodeFormat, getId(m), pid, getName(m), attrstr);
                    }
                }
            }

            if (!string.IsNullOrEmpty(rootNode)) //根节点
            {
                html = string.Format("{{id:null,pId:'',name:'{0}'}},", rootNode) + html;
            }
            html = "[" + html.TrimEnd(',') + "]";

            if (!string.IsNullOrEmpty(varname))
            {
                html = string.Format("var {0}={1}", varname, html);
            }
            return html;
        }
    }
}